package com.xinqizhi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinqizhi.entity.SysMenuEntity;
import com.xinqizhi.entity.SysRoleMenu;
import com.xinqizhi.service.SysMenuService;
import com.xinqizhi.service.impl.SysRoleMenuServiceImpl;
import com.xinqizhi.vo.MenuNotInRoleVo;
import com.xinqizhi.vo.RoleMenuVo;
import com.xinqizhi.vo.SimpleVo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**  
 * @ClassName: SysRoleMenuController  
 * @Description: ${todo}(控制器)  
 * @author yangtongquan  
 * @date 2019-05-21  
 *  
 */
@RestController
@RequestMapping("/sys")
public class SysRoleMenuController   {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private SysRoleMenuServiceImpl sysRoleMenuService;
	@Autowired
	private SysMenuService menuService;
	/**
	 *先查询所有已经关联的菜单，并由role-id查询
	 */
	@RequestMapping("/getrolemenu")
	@ResponseBody
	public Map selectMenusByRoleId(RoleMenuVo roleMenuVo,Integer roleId) {
		logger.info("------------------------role-id查询关联--roleId=" +roleId+ ",,,分页参数=" + roleMenuVo.toString() + roleMenuVo.getPaging());
		Map result = new HashMap<String, Object>(10);
		if (roleId == null) {
			result.put("msg", "参数roleId不合法");
			return result;
		}
		if (roleMenuVo.getPaging()) {
			PageHelper.startPage(roleMenuVo.getPage(), roleMenuVo.getLimit());
		}
		List<RoleMenuVo> roleMenuVos = sysRoleMenuService.getRole2MenuByRoleId(roleId);
		PageInfo<RoleMenuVo> roleMenuPage = new PageInfo(roleMenuVos);
		result.put("count", roleMenuPage.getTotal());
		result.put("data", roleMenuPage.getList());
		result.put("code", 0);
		result.put("msg", "查询数据成功");
		result.put("success", true);
		return result;
	}

	/**
	 * 为角色添加关联菜单
	 * @param
	 * @return
	 * var midlist = [];
	 * midlist.push(value);
	 * ajax的data属性
	 *     data:{
	 *        'midlist':midlist
	 *      }
	 *  controller以集合接收
	 * public Map addRoleMenu(@RequestParam("midlist[]") List<Integer> midlist){
	 *
	 */
	@RequestMapping("/addRoleMenu")
	@ResponseBody
	public Map addRoleMenu(@RequestParam("roleId")Integer roleId,@RequestParam("mids[]") List<Integer> midlist){
		HashMap<String, Object> result = new HashMap<>(10);
		logger.info(roleId + "--------------------------" + midlist);
		if (roleId == null || midlist.isEmpty()) {
			result.put("msg", "参数请求错误");
			result.put("success", false);
			return result;
		}
		try {
			//先查询该角色有的菜单id,移除已经关联有的ID
			List<Integer> menuIds = sysRoleMenuService.selectMenuIdByRoleId(roleId);
			if (menuIds.isEmpty()){
				for (Integer id : midlist) {
					if (menuIds.contains(id)) {
						midlist.remove(id);
					}
				}
			}
			Integer flag = null;
			if (midlist.isEmpty()) {
				result.put("success", false);
				result.put("msg", "所选的数据已经存在");
			} else {
				flag = sysRoleMenuService.addRoleIdByMenuLsit(roleId, midlist);
				result.put("success", true);
				result.put("msg", "成功添加" + flag.toString() + "条数据");
			}
		} catch (Exception e) {
			result.put("success", false);
			result.put("code", 500);
			result.put("msg", "添加数据异常" + StringUtils.substring(e.getMessage(), 0, 1000));
		}
		return result;
	}

    /**
     * 批量删除菜单角色关联
     * @param roleMenuVos
     * @return
     */
    @RequestMapping("/batchDeleteRoleMenu")
    @ResponseBody
    public Map batchDeleteRoleMenu(@RequestBody List<RoleMenuVo> roleMenuVos){
        HashMap<String, Object> result = new HashMap<>(10);
        logger.info(roleMenuVos+"--------------------------"+roleMenuVos);
        if (roleMenuVos == null || roleMenuVos.isEmpty()) {
            result.put("msg","参数请求错误");
            result.put("success",false);
            return result;
        }
        try {
            Integer roleMenu = sysRoleMenuService.batchDeleteRoleMenu(roleMenuVos);
            if (roleMenu >0){
                result.put("msg","成功删除"+roleMenu.toString()+"条数据");
                result.put("success",true);
            }else {
                result.put("msg","删除失败");
                result.put("success",false);
            }
        }catch (Exception e){
            result.put("msg","系统异常"+StringUtils.substring(e.getMessage(),0,1000));
            result.put("success",false);
        }
        return result;
    }

	/**
	 * 先加载某个角色没有的菜单列表
	 * @return
	 */
	@RequestMapping("/getRoleNotHaveMenus")
	@ResponseBody
	public Map getRoleNotHaveMenus(SysMenuEntity entity,@RequestParam("roleId")Integer roleId){
		logger.info("分页传参+++="+entity.toString()+entity.getPaging());
		Map<String, Object> result = new HashMap<>(10);
		if (entity.getPaging()){
			PageHelper.startPage(entity.getPage(),entity.getLimit());
		}
		List<SysMenuEntity> list = menuService.getRoleNotHaveMenus(roleId);
		PageInfo page = new PageInfo(list);
		result.put("data",page.getList());
		result.put("count",page.getTotal());
		result.put("succes",true);
		result.put("code",0);
		result.put("msg","查询所有菜单成功");
		return result;
	}
}
